<?php
namespace Admin\Controller;

/**
 * 后台配置控制器
 * @author molong <zuojiazi@vip.qq.com>
 */
class LinkController extends AdminController {

	public function _initialize(){
		$this->assign('_extra_menu',array(
		    '已装插件后台'=> D('Addons')->getAdminList(),
		));

		parent::_initialize();
	}

	public function index(){
		$link = D('Friendlink');

		$data = $link->where($map)->order('list desc')->select();
		$build = new \OT\Builder();

		$build->title("友情链接")
			->buttonNew(U('add'))
			->keyId()->keyTitle()
			->keyText('update_time','更新时间')
			->keyDoAction('edit?id=###','编辑')
			->keyDoAction('delete?id=###','删除')
			->data($data)
			->display();
	}

	//添加
	public function add(){
		$link = D('Friendlink');
		if(IS_POST){
			$data = array(
				'title'   => I('title'),
				'url'   => I('url'),
				'cover_id'   => I('cover_id'),
				'list'   => I('list'),
				'descrip'   => I('descrip'),
				'update_time' => date('Y-m-d')
			);
			$result = $link->add($data);
			if ($result) {
				$this->success("添加成功！",U('Link/index'));
			}else{
				$this->error($link->getError());
			}
		}else{
			$build = new \OT\Builder('config');

			$build->title("添加链接")
				->keyText('title','站点名称')
				->keyText('url','站点地址')
				->keySingleImage('cover_id','站点LOGO')
				->keyText('list','排序')
				->keyTextArea('descrip','备注')
				->data($data)
				->buttonSubmit()
				->buttonBack()
				->display();
		}
	}

	//修改
	public function edit(){
		$link = D('Friendlink');
		$id = I('get.id','','trim,intval');
		if(IS_POST){
			$data = array(
				'title'   => I('title'),
				'url'   => I('url'),
				'cover_id'   => I('cover_id'),
				'list'   => I('list'),
				'descrip'   => I('descrip'),
				'update_time' => date('Y-m-d')
			);
			$result = $link->where(array('id'=>$id))->save($data);
			if ($result) {
				$this->success("修改成功！",U('Link/index'));
			}else{
				$this->error("修改失败！");
			}
		}else{
			$map = array('id'=>$id);
			$data = $link->where($map)->find();
			$build = new \OT\Builder('config');

			$build->title("修改链接")
				->keyText('title','站点名称')
				->keyText('url','站点地址')
				->keySingleImage('cover_id','站点LOGO')
				->keyText('list','排序')
				->keyTextArea('descrip','备注')
				->data($data)
				->buttonSubmit()
				->buttonBack()
				->display();
		}
	}

	//删除
	public function delete($id){
		$link = D('Friendlink');

		$map = array('id'=>$id);
		$result = $link->where($map)->delete();
		if ($result) {
			$this->success("删除成功！");
		}else{
			$this->error("删除失败！");
		}
	}
}